<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>新闻系统 - 用户登录</title>
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .login-card {
            max-width: 400px;
            margin: 5rem auto;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
            border-radius: 15px;
        }

        .news-logo {
            width: 80px;
            height: 80px;
            background: #0d6efd;
            border-radius: 50%;
            margin: -40px auto 20px;
        }
    </style>
</head>

<body class="bg-light">
    <div class="container">
        <div class="card login-card">
            <div class="card-body p-5">
                <div class="news-logo"></div>
                <h2 class="text-center mb-4">新闻系统登录</h2>

                <!-- 登录表单 -->
                <form id="loginForm" novalidate>
                    <!-- 账号输入 -->
                    <div class="mb-3">
                        <label for="username" class="form-label">账号</label>
                        <input type="text" class="form-control" id="username" name="username" required
                            placeholder="请输入您的账号">
                        <div class="invalid-feedback">请输入有效账号</div>
                    </div>

                    <!-- 密码输入 -->
                    <div class="mb-4">
                        <label for="password" class="form-label">密码</label>
                        <input type="password" class="form-control" id="password" name="password" required
                            placeholder="请输入密码">
                        <div class="invalid-feedback">请输入密码</div>
                    </div>

                    <!-- 登录按钮 -->
                    <button type="submit" class="btn btn-primary w-100 mb-3" id="loginBtn">
                        立即登录
                    </button>

                    <!-- 注册链接 -->
                    <div class="text-center">
                        <span>没有账号？</span>
                        <a href="/register.html" class="btn btn-link p-0" id="registerLink">
                            立即注册
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Bootstrap 5 JS 及其依赖 -->
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/js/bootstrap.bundle.min.js"></script>

    <!-- 自定义表单处理脚本 -->
    <script>
        (function () {
            'use strict'

            // 获取表单元素
            const form = document.getElementById('loginForm')
            const loginBtn = document.getElementById('loginBtn')

            // 表单提交处理
            form.addEventListener('submit', async function (event) {
                event.preventDefault()
                event.stopPropagation()

                if (!form.checkValidity()) {
                    form.classList.add('was-validated')
                    return
                }

                try {
                    // 禁用按钮防止重复提交
                    loginBtn.disabled = true
                    loginBtn.innerHTML = `
                        <span class="spinner-border spinner-border-sm" aria-hidden="true"></span>
                        登录中...
                    `

                    // 准备请求数据
                    const payload = {
                        name: document.getElementById('username').value.trim(),
                        passwd: document.getElementById('password').value
                    }

                    // 发送登录请求
                    const response = await fetch('/login', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify(payload)
                    })

                    if (!response.ok) {
                        throw new Error(`登录失败: ${response.status}`)
                    }

                    const result = await response.json()
                    if (result.redirect_url) {
                        alert("登录成功，即将跳转到主页...")
                        window.location.href = result.redirect_url  // 使用 result.redirect_url
                    }
                } catch (error) {
                    // 错误处理
                    alert(error.message || '登录请求失败，请稍后重试')
                } finally {
                    // 恢复按钮状态
                    loginBtn.disabled = false
                    loginBtn.innerHTML = '立即登录'
                }
            })
        })()
    </script>
</body>

</html>
